package org.crmsystem.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.crmsystem.dao.UserMapper;
import org.crmsystem.model.Page;
import org.crmsystem.model.User;
import org.crmsystem.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 用户服务层实现
 * @author 孙艳平
 *
 */
@Service
public class UserServiceImpl implements IUserService {
	@Autowired
	private UserMapper userMapper;
	/**
	 * 用户登录
	 */
	public User userLogin(User user) {
		return userMapper.userLogin(user);
	}

	/**
	 * 根据id获取用户详细信息
	 */
	public User getUserById(int id) {
		return userMapper.getUserById(id);
	}

	/**
	 * 修改用户密码
	 * @param map
	 * @return
	 */
	public int updateUserPassword(Map map) {
		return userMapper.updateUserPassword(map);
	}

	/**
	 * 多条件查询获取用户列表
	 * @param map
	 * @return
	 */
	public Page getUser(Map map) {
		User user=(User)map.get("user");
		Page page=new Page();
		Map map1=new HashMap();
		map1.put("user",user);
		map1.put("startDate",map.get("startDate"));
		map1.put("endDate",map.get("endDate"));
		int totalNum=userMapper.userCount(map1);
		page.setTotalNum(totalNum);
		int pageSize=Integer.parseInt(String.valueOf(map.get("pageSize")));
		page.setPageSize(pageSize);
		int currPage=Integer.parseInt(String.valueOf(map.get("currPage")));
		page.setCurrPage(currPage);
		page.setResult(userMapper.getUser(map));
		page.setTotalPage(totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1);
		return page;
	}

	/**
	 * 添加用户
	 */
	public int addUser(User user) {
		return userMapper.addUser(user);
	}

	/**
	 * 删除用户
	 */
	public int delUser(int id) {
		return userMapper.delUser(id);
	}

	/**
	 * 更新用户
	 */
	public int updateUser(User user) {
		return userMapper.updateUser(user);
	}
	/**
	 * 根据用户名获得用户
	 */
	public User getUserByName(String name) {
		return userMapper.getUserByName(name);
	}
}
